/**
 * The main application class. An instance of this class is created by app.js when it
 * calls Ext.application(). This is the ideal place to handle application launch and
 * initialization details.
 */
Ext.ariaWarn = Ext.emptyFn;
Ext.tip.QuickTipManager.init();

function loadLocale() {

    var lang = sessionStorage ? (sessionStorage.getItem('user-lang') || 'zh_CN') : 'zh_CN',
        file = Ext.util.Format.format("resources/local/{0}.js", lang);

    Ext.Loader.loadScript({
        url: file,
        onError: function() {
            alert('Error loading locale file. Please contact system administrator.');
        }
    });
}


Ext.define('DragonApp.Application', {
    extend: 'Ext.app.Application',
    quickTips: true,

    name: 'DragonApp',

    stores: [
        // TODO: add global / shared stores
    ],

    requires: [
        'Ext.Img',
        'Ext.window.Toast',
        'Ext.container.Viewport',

        'Ext.ux.TabCloseMenu',

        'DragonApp.util.Util',
        'DragonApp.util.Glyphs',
        'DragonApp.util.String',

        'DragonApp.ux.config.Config',

        'DragonApp.view.login.Login',
        'DragonApp.view.main.Main',

        'DragonApp.view.basic.Container',
        'DragonApp.view.hero.Container',
        'DragonApp.view.monster.Container',
        'DragonApp.view.pack.Container',
        'DragonApp.view.journal.Container',

        'DragonApp.view.base.Container',
        'DragonApp.view.level.Container',
        'DragonApp.view.order.Container',
        'DragonApp.view.dataconsume.Container',

        'DragonApp.view.property.Container',
        'DragonApp.view.notice.Container',
        'DragonApp.view.broadcast.Container',
        'DragonApp.view.ban.Container',
        'DragonApp.view.white.Container',
        'DragonApp.view.gift.Container',
        'DragonApp.view.resource.Container',

        'DragonApp.view.user.Grid',
        'DragonApp.view.operalog.Container',
        'DragonApp.view.cron.Container',

        'Ext.overrides.locale.zh_CN.view.AbstractView',
        'DragonApp.store.JsonPServer'
    ],

    launch: function() {

        var me = this;
        var task, loggedIn;

        loggedIn = sessionStorage.getItem("DragonApp");

        task = new Ext.util.DelayedTask(function() {

            me.splashscreen.fadeOut({
                duration: 1000,
                remove: true,
                listeners: {
                    afteranimate: function(el, startTime, eOpts) {

                        //根据登录状态显示界面
                        Ext.create({
                            xtype: loggedIn ? 'app-main' : 'login'
                        });

                        if (loggedIn) {
                            DragonApp.ux.session.SessionMonitor.start();
                        }
                    }
                }
            });
        });

        task.delay(2000);

        DragonApp.util.String.extendTrim();
        DragonApp.util.Array.noEmpty();
    },

    init: function() {
        var me = this;
        me.splashscreen = Ext.getBody().mask(
            'Loading application', 'splashscreen'
        );
        me.splashscreen.addCls('splashscreen');
        Ext.DomHelper.insertFirst(Ext.query('.x-mask-msg')[0], {
            cls: 'x-splash-icon'
        });
    },


    onAppUpdate: function() {

        var updateWindow = Ext.create('Ext.window.Window', {
            bodyPadding: 5,
            closable: false,
            closeAction: 'hide',
            modal: true,
            resizable: false,
            title: '运营后台升级提示',
            width: 330,
            items: [{
                xtype: 'container',
                frame: true,
                html: '<div style="padding: 15px">程序已更新, 重新加载?</div>'
            }],
            buttons: [{
                text: '是',
                handler: function() {

                    window.location.reload();
                }
            }, {
                text: '否',
                //action: 'logout',
                handler: function() {

                }
            }]
        }).show();
    }
});